package com.baizhi.service.impl;

import com.baizhi.dao.CmfzLessonDao;
import com.baizhi.entity.CmfzLesson;
import com.baizhi.service.CmfzLessonService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2019/4/10 0010.
 */
@Service
public class CmfzLessonServiceImpl implements CmfzLessonService {
    @Autowired
    private CmfzLessonDao cmfzLessonDao;
    private Logger logger= LoggerFactory.getLogger(CmfzLessonServiceImpl.class);

    @Override
    public Map showAll(Integer page, Integer rows, Integer lessonStatus) {
        Map map = new HashMap();

        IPage<CmfzLesson> iPage = new Page<>(page, rows);
        QueryWrapper<CmfzLesson> wrapper = new QueryWrapper<>();
        if (lessonStatus != null && !"".equals(lessonStatus)) {
            wrapper.eq("lesson_status", lessonStatus);
        } else {
            wrapper = null;
        }
        IPage<CmfzLesson> cmfzLessonIPage = cmfzLessonDao.selectPage(iPage, wrapper);
        List<CmfzLesson> records = cmfzLessonIPage.getRecords();
        long total = cmfzLessonIPage.getTotal();

        map.put("total", total);
        map.put("rows", records);
        return map;
    }

    @Override
    public Map add(CmfzLesson cmfzLesson) {
        Map map = new HashMap();

        try {
            cmfzLessonDao.insert(cmfzLesson);
            map.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", false);
        }

        return map;
    }

    @Override
    public Map delete(int[] ids) {
        /*将数组转成List集合*/
        List<Integer> list = new ArrayList<>();
        for (int i : ids) {
            list.add(i);
        }
        logger.debug("!!!!!!!!!!!!!!!!!!!!!1{}",list);
        Map map = new HashMap();

        try {
            cmfzLessonDao.deleteBatchIds(list);
            map.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag", false);
        }

        return map;
    }
}
